package com.sunlands.zlcx.usercenter.util;

import com.github.qcloudsms.SmsSenderUtil;
import com.github.qcloudsms.SmsSingleSender;
import com.github.qcloudsms.SmsSingleSenderResult;
import com.github.qcloudsms.httpclient.HTTPException;
import com.sunlands.gateway.KongClient;
import com.sunlands.zlcx.usercenter.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import okhttp3.Response;
import okhttp3.ResponseBody;
import org.json.JSONException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.*;

/**
 * @author shixiaopeng
 */
@Component
@Slf4j
public class TencentSms {


    // 短信应用SDK AppID
    @Value("${tencent.sms.appid}")
    private String appid; // 1400开头

    // 短信应用SDK AppKey
    @Value("${tencent.sms.appkey}")
    private String appkey;

    // 短信模板ID，需要在短信应用中申请
    @Value("${tencent.sms.verifyCodeId}")
    private String verifyCodeId; // NOTE: 这里的模板ID`7839`只是一个示例，真实的模板ID需要在短信控制台中申请

    // 短信模板ID，需要在短信应用中申请
    @Value("${tencent.sms.reportId}")
    private String reportId; // NOTE: 这里的模板ID`7839`只是一个示例，真实的模板ID需要在短信控制台中申请

    // 签名
    @Value("${tencent.sms.sign}")
    private String sign; // NOTE: 这里的签名"腾讯云"只是一个示例，真实的签名需要在短信控制台中申请，另外签名参数使用的是`签名内容`，而不是`签名ID`

    public String sendVerifyCode(String phoneNum, ArrayList<String> params){

        return this.sendSms(phoneNum, params, Integer.parseInt(verifyCodeId));
    }

    public String sendReport(String phoneNum, ArrayList<String> params){

        return this.sendSms(phoneNum, params, Integer.parseInt(reportId));
    }

    public String sendSms(String phoneNum, ArrayList<String> params,int templateId){

        String resultCode = "0";
        try {
            SmsSingleSender ssender = new SmsSingleSender(Integer.parseInt(appid), appkey);
            SmsSingleSenderResult result = ssender.sendWithParam("86", phoneNum,
                    templateId,  params, sign, "", "");  // 签名不能为空串
            log.info("腾讯云短信 result = {}", result);
            //{"result":0,"errmsg":"OK","ext":"","sid":"2019:2536734410286720918","fee":1}
            resultCode = ""+ result.result;
        } catch (HTTPException e) {
            // HTTP响应码错误
            e.printStackTrace();
        } catch (JSONException e) {
            // json解析错误
            e.printStackTrace();
        } catch (IOException e) {
            // 网络IO错误
            e.printStackTrace();
        }
        return resultCode;
    }

    public static long getRandom() {
        return (new Random(SmsSenderUtil.getCurrentTime())).nextInt(900000) + 100000;
    }

    public static void main(String[] args) throws IOException {
        try {
            String[] params = {"5678","2"};
            SmsSingleSender ssender = new SmsSingleSender(1400311459, "d17784f55a7d72c252c4d325ce8656c8");
            SmsSingleSenderResult result = ssender.sendWithParam("86", "13718348719",
                    530606, params, "同桌APP", "", "");  // 签名不能为空串
            System.out.println(result);

        } catch (HTTPException e) {
            // HTTP响应码错误
            e.printStackTrace();
        } catch (JSONException e) {
            // json解析错误
            e.printStackTrace();
        } catch (IOException e) {
            // 网络IO错误
            e.printStackTrace();
        }
    }
}
